using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;

using System.IO;
using System.Drawing;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms
{
    public partial class ImageView : BasePage
    {
        private int MaxDim = 400;
        protected void Page_Load(object sender, EventArgs e)
        {
            string file="";
            if (!IsPostBack)
            {
                long id = Convert.ToInt64(Request["id"]);
               
                if (Request["mdim"] != null)
                {
                    MaxDim = Convert.ToInt32(Request["mdim"]);
                }


                if (Request["forum"] != null)
                {
                    file = HttpContext.Current.Server.MapPath(FolderForumVirtualPath+Request["forum"]);
                }
                else
                {
                    if (Request["photo"] != null)
                    {
                        file = Path.Combine(FolderPhotoPhysicalPath, Request["photo"]);
                    }
                    else
                    {
                        if (Request["ad"] != null)
                        {
                            file = Path.Combine(FolderAdPhysicalPath, Request["ad"]);
                        }
                        else
                        {
                            if (Request["img"] != null)
                            {
                                file = Path.Combine(FolderUserProfilePhysicalPath, Request["img"]);
                            }
                            else
                            {
                            
                                IResourceFile resourceFile = ResourceFileController.GetResourceFile(id);
                                if (resourceFile != null)
                                    file = Path.Combine(FolderUploadPhysicalPath, String.Format("{0}{1}", id, resourceFile.Extention));
                                
                            }
                        }
                    }
                }


               // file = "e:/img2.jpg";
                if (File.Exists(file))
                {

                    using (Image img = new Bitmap(file))
                    {

                        int h = img.Height;
                        int w = img.Width;
                        int b = h > w ? h : w;

                        if (Math.Max(h, w) <= MaxDim)
                        {
                            img.Save(Response.OutputStream,
                                    System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        else
                        {

                            double per = (b > MaxDim) ? (MaxDim * 1.0) / b : 1.0;
                            h = (int)(h * per);
                            w = (int)(w * per);


                            using (Image img2 =
                                       img.GetThumbnailImage(w, h,
                                       new Image.GetThumbnailImageAbort(Abort), IntPtr.Zero))
                            {

                                Response.ContentType = "image/jpeg";
                                img2.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                }
            }
        }
        private bool Abort()
        {
            return false;
        }
    }
}
